<template>
  <van-form @submit="onsubmit">
    <van-cell-group inset>
      <van-field v-model="userAccount"
                 name="userAccount"
                 label="账号"
                 placeholder="请输入账号"
                 :rules="[{required:true,message:'请填写用户名'}]"
      />
      <van-field v-model="userPassword"
                 name="userPassword"
                 type="password"
                 label="密码"
                 placeholder="请输入密码"
                 :rules="[{required:true,message:'请填写密码'}]"
      />
    </van-cell-group>
    <div style="margin: 10px">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>
</template>

<script setup lang="ts">
import {ref} from "vue";
import {useRoute, useRouter} from "vue-router";
import myAxios from "../plugins/myAxios";
import {showFailToast, showSuccessToast} from "vant";

const userAccount = ref("");
const userPassword = ref("");
const route = useRoute();
const onsubmit = async () => {
  const res = await myAxios.post('/user/login', {
    userAccount: userAccount.value,
    userPassword: userPassword.value,
  })
  console.log(res, "用户登录")
  if (res.code === 0 && res.data) {
    showSuccessToast('登录成功')
    window.location.href = route.query?.redirectPath as string ?? '/';
  } else {
    showFailToast('登录失败')
  }
}
</script>


<style scoped>

</style>